import ListNode  from "../common/list_node"
import {a1,b1} from '../common/list_node'
//看懂后豁然开朗，其实就是让2个指针走**一样**的距离，消除步行差，那就一定可以一起走到相交点.走一样的距离
function getIntersectionNode(headA:ListNode | null , headB: ListNode | null): ListNode | null {
  if(headA == null || headB == null) return null
  let h1:ListNode | null = headA
  let h2:ListNode | null = headB
  while(h1 != h2) {
    h1 = (h1 == null ? headB : h1.next)
    h2 = (h2 == null ? headA : h2.next)
  }
  return h1
};

function getIntersectionNode_hash(headA:ListNode | null , headB: ListNode | null): ListNode | null {
  if(headA == null || headB == null) return null
  let hashTable = new Set()
  let tmp:ListNode | null = headA
  while(tmp != null) {
    hashTable.add(tmp)
    tmp = tmp.next
  }
  tmp = headB
  while(tmp != null) {
    if(hashTable.has(tmp)) {
      return tmp
    }
    tmp = tmp.next
  }
  return null
}



let find = getIntersectionNode_hash(a1,b1)
console.log(find?.val)